#include "common.h"
#include <QDialog>
#include <QMessageBox>
#include <QDateTime>
#include <QRandomGenerator>
#include <QRegularExpression>
#include <QFile>
#include <QAbstractButton>

Common::Common() {
    loadQssfromfile(this,":/qss/style.qss");
}

bool Common::createMessageBox(QWidget *widget,const QString type,const QString msg){  //生成弹窗
    if(type.compare("错误")==0){
        QMessageBox::critical(this,type,msg);
    }else if(type.compare("警告")==0){
        QMessageBox::warning(this,type,msg);
    }else if(type.compare("提示")==0){
        QMessageBox::information(this,type,msg);
    }else if(type.compare("选择")==0){
        int ret =QMessageBox::information(this,"提示",msg,QMessageBox::Yes | QMessageBox::No);
        if(ret==QMessageBox::No){
            return false;
        }
    }
    return true;
}

bool Common::createMessageBox2(const QString type,const QString msg){  //生成弹窗
    qDebug()<<"DEBUG: createMessageBox";
    QMessageBox messageBox;
    messageBox.setWindowFlags(messageBox.windowFlags() & ~Qt::WindowCloseButtonHint); // 禁用系统关闭按钮

    messageBox.setText(msg);
    if(type.compare("错误")==0){
        messageBox.setWindowTitle("错误");
        messageBox.setIcon(QMessageBox::Critical); // 信息图标
        messageBox.addButton(QMessageBox::Yes);
    }else if(type.compare("警告")==0){
        messageBox.setWindowTitle("警告");
        messageBox.setIcon(QMessageBox::Warning); // 信息图标
        messageBox.addButton(QMessageBox::Yes);
    }else if(type.compare("提示")==0){
        messageBox.setWindowTitle("提示");
        messageBox.setIcon(QMessageBox::Information); // 信息图标
        messageBox.addButton(QMessageBox::Yes);
    }else if(type.compare("选择")==0){
        messageBox.setWindowTitle("提示");
        messageBox.setIcon(QMessageBox::Question);
        messageBox.addButton(QMessageBox::Yes);
        messageBox.addButton(QMessageBox::No);
    }
    int response = messageBox.exec();
    if(response==QMessageBox::No){
        return false;
    }
    return true;
}

QString Common::getDateTime(const QString format,int anyDays){
    // 获取当前时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // 添加5天
    QDateTime anyDateTime = currentDateTime.addDays(anyDays);
    QString dateTime = anyDateTime.toString(format);
    return dateTime;
}

QString Common::getRandomDoubleStr(double min, double max, int decimalPlaces,bool percent){
    double  randomDouble = QRandomGenerator::global()->generateDouble()*(min-max)+max;
    QString randomDoubleStr;
    if (percent){
        randomDoubleStr = QString("%1%").arg(randomDouble * 100, 0, 'f', decimalPlaces);
    }else{
        randomDoubleStr = QString("%1").arg(randomDouble , 0, 'f', decimalPlaces);
    }
    return randomDoubleStr;
}

double Common::getRandomDouble(double min, double max){
    double  randomDouble = QRandomGenerator::global()->generateDouble()*(min-max)+max;
    return randomDouble;
}

QString Common::getRandomIntStr(int min, int max, bool percent){
    int randomInt = QRandomGenerator::global()->bounded(min,max);
    QString randomIntStr;
    if (percent){
        randomIntStr = QString("%1%").arg(randomInt*100 , 0);
    }else{
        randomIntStr = QString("%1").arg(randomInt , 0);
    }
    return randomIntStr;
}

int Common::getRandomInt(int min, int max){
    int randomInt = QRandomGenerator::global()->bounded(min,max); // 生成一个随机整数
    return randomInt;
}

bool Common::matchRegularExpression(const QString format,const QString text){
    QRegularExpression regExp(format); // 正则表达式
    QRegularExpressionMatch match = regExp.match(text); //匹配表达式
    if (match.hasMatch()) {
        qDebug() << "符合正则表达式";
        return true;
    } else {
        qDebug() << "不符合正则表达式";
        return false;
    }


}

void Common::loadQssfromfile(QWidget *widget,const QString filePath){
    QFile dataMonitorFile(filePath);
    if (dataMonitorFile.open(QFile::ReadOnly)) {
        QString styleSheet = QLatin1String(dataMonitorFile.readAll());
        widget->setStyleSheet(styleSheet);
    }else{
        qDebug() <<"UserManger QSS打开失败";
    }
}

